Ext.define('Cps.view.LoginForm', {
    extend: 'Ext.form.Panel',
    alias : 'widget.loginform',
    
    title: Cps.Locale.Login.title,
    frame: true,
    width: 320,
    bodyPadding: 10,
    
    defaultType: 'textfield',
    defaults: {
        anchor: '100%',
        labelWidth: 120
    },
    
    url: baseUrl + '/user/login',
    
    items: [
        {
            allowBlank: false,
            fieldLabel: Cps.Locale.Login.field_code,
            name: 'UserLogin[username]',
            emptyText: Cps.Locale.Login.field_code,
            listeners: {
                specialkey: function(field, e){
                    // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
                    // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
                    if (e.getKey() == e.ENTER) {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                success: function(form, action) {
									localStorage.userName = action.result.userName;
                                    location.href = action.result.returnUrl + "/";
                                },
                                failure: function(form, action) {
                                    Ext.Msg.alert(Cps.Locale.Login.login_title_message, Cps.Locale.Login.login_fail);
                                }
                            });
                        }
                    }
                }
            }
        },
        {
            allowBlank: true,
            fieldLabel: Cps.Locale.Login.field_password,
            name: 'UserLogin[password]',
            emptyText: Cps.Locale.Login.field_password,
            inputType: 'password',
            listeners: {
                specialkey: function(field, e){
                    // e.HOME, e.END, e.PAGE_UP, e.PAGE_DOWN,
                    // e.TAB, e.ESC, arrow keys: e.LEFT, e.RIGHT, e.UP, e.DOWN
                    if (e.getKey() == e.ENTER) {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                success: function(form, action) {
									localStorage.userName = action.result.userName;
                                    location.href = action.result.returnUrl + "/";
                                },
                                failure: function(form, action) {
                                    Ext.Msg.alert(Cps.Locale.Login.login_title_message, Cps.Locale.Login.login_fail);
                                }
                            });
                        }
                    }
                }
            }
        }/*,
        {
        	xtype: 'combobox',
        	fieldLabel: Cps.Locale.Login.field_language,
        	name: 'language',
        	store: Ext.create('Ext.data.Store', {
        		fields: [ 'code', 'name' ],
        		data: [
        		    { code: 'en', name: Cps.Locale.ENGLISH },
        		    { code: 'fr', name: Cps.Locale.FRENCH }
        		]
        	}),
        	displayField: 'name',
        	valueField: 'code',
    		editable: false,
    		value: 'en'
        }*/
    ],
    
    buttons: [{
		    id: "btnLogin",
				text:Cps.Locale.Login.btn_login,
				action: 'login',
				formBind: true, //only enabled once the form is valid
	      disabled: true,
	      handler: function() {
	          var form = this.up('form').getForm();
	          if (form.isValid()) {
	              form.submit({
	                  success: function(form, action) {
						localStorage.userName = action.result.userName;
	                     location.href = action.result.returnUrl + "/";
	                  },
	                  failure: function(form, action) {
	                      Ext.Msg.alert(Cps.Locale.Login.login_title_message, Cps.Locale.Login.login_fail);
	                  }
	              });
	          }
  			}
		}]
});
